package sale.wxb.loveshopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import sale.wxb.loveshopping.constant.RedisConst;
import sale.wxb.loveshopping.entity.common.ResultException;
import sale.wxb.loveshopping.entity.model.CommodityClassify;
import sale.wxb.loveshopping.dao.CommodityClassifyDao;
import sale.wxb.loveshopping.entity.vo.commodity.SaveCommodityClassifyVo;
import sale.wxb.loveshopping.entity.vo.common.PageListParam;
import sale.wxb.loveshopping.entity.vo.common.PageRecord;
import sale.wxb.loveshopping.service.ICommodityClassifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sale.wxb.loveshopping.utils.PageRecordUtil;
import sale.wxb.loveshopping.utils.RedisUtil;

/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author 王小宝
 * @since 2022-12-02
 */
@Transactional
@Service
public class CommodityClassifyServiceImpl extends ServiceImpl<CommodityClassifyDao, CommodityClassify> implements ICommodityClassifyService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public CommodityClassify getDetail(Long id) {
        return this.getById(id);
    }

    @Override
    public Long saveClassify(SaveCommodityClassifyVo body) {
        CommodityClassify commodityClassify;

        if (ObjectUtils.isEmpty(body.getId()) || body.getId().equals(0L)) {
            commodityClassify = new CommodityClassify();
        } else {
            commodityClassify = this.getById(body.getId());

            if (ObjectUtils.isEmpty(commodityClassify)) {
                throw new ResultException("该记录不存在");
            }
        }

        commodityClassify.setEnabled(body.getEnabled());
        commodityClassify.setName(body.getName());
        commodityClassify.setSort(body.getSort());
        commodityClassify.setParentId(body.getParentId() == null ? 0 : body.getParentId());
        commodityClassify.setIcon(body.getThumb());

        if (!this.saveOrUpdate(commodityClassify)) {
            throw new ResultException("保存失败");
        }

        // 移除缓存
        redisUtil.del(RedisConst.COMMODITY_CLASSIFY);
        return commodityClassify.getId();
    }

    @Override
    public Boolean deleteClassify(Long id) {
        return this.removeById(id);
    }

    @Override
    public PageRecord<CommodityClassify> listByPage(PageListParam listParam) {
        Page<CommodityClassify> page = new Page<>(listParam.getPage(), listParam.getSize());
        LambdaQueryWrapper<CommodityClassify> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(listParam.getKeyword()), CommodityClassify::getName, listParam.getKeyword());
        return PageRecordUtil.toPageRecord(this.page(page));
    }
}
